package com.edu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.entity.User;
import com.edu.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserLogInServlet
 */
@WebServlet(description = "logInDo", urlPatterns = { "/logInDo" })
public class UserLogInServlet extends HttpServlet {
	UserServiceImpl usi = new UserServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("用户名为："+username);
		System.out.println("密码为："+password);
		User u = new User();
		u.setUserName(username);
		u.setPassword(password);
		User logInUser = usi.userLogIn(u);
		if(logInUser != null) {
			System.out.println("登录成功");
			HttpSession session = request.getSession();
			session.setAttribute("logInUser", logInUser);
			response.sendRedirect("goods.jsp");
		}else {
			System.out.println("登录失败");
			response.sendRedirect("login.jsp");
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
